

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>kospeech.data.audio.parser &mdash; KoSpeech 0.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> KoSpeech
          

          
          </a>

          
            
            
              <div class="version">
                0.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">NOTES</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/intro.html">Intro</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/Preparation.html">Preparation before Training</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/opts.html">Options</a></li>
</ul>
<p class="caption"><span class="caption-text">ARCHITECTURE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../Seq2seq.html">Seq2seq</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Transformer.html">Transformer</a></li>
</ul>
<p class="caption"><span class="caption-text">PACKAGE REFERENCE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../Checkpoint.html">Checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Data.html">Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Decode.html">Decode</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Evaluator.html">Evaluator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Optim.html">Optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Trainer.html">Trainer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Etc.html">Etc</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">KoSpeech</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>kospeech.data.audio.parser</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for kospeech.data.audio.parser</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">torch</span> <span class="k">import</span> <span class="n">Tensor</span><span class="p">,</span> <span class="n">FloatTensor</span>
<span class="kn">from</span> <span class="nn">kospeech.data.audio.core</span> <span class="k">import</span> <span class="n">load_audio</span>
<span class="kn">from</span> <span class="nn">kospeech.data.audio.augment</span> <span class="k">import</span> <span class="n">NoiseInjector</span><span class="p">,</span> <span class="n">SpecAugment</span>
<span class="kn">from</span> <span class="nn">kospeech.data.audio.feature</span> <span class="k">import</span> <span class="n">MelSpectrogram</span><span class="p">,</span> <span class="n">MFCC</span><span class="p">,</span> <span class="n">Spectrogram</span><span class="p">,</span> <span class="n">FilterBank</span>


<div class="viewcode-block" id="AudioParser"><a class="viewcode-back" href="../../../../Data.html#kospeech.data.audio.parser.AudioParser">[docs]</a><span class="k">class</span> <span class="nc">AudioParser</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Provides inteface of audio parser.</span>

<span class="sd">    Note:</span>
<span class="sd">        Do not use this class directly, use one of the sub classes.</span>

<span class="sd">    Method:</span>
<span class="sd">        - **parse_audio()**: abstract method. you have to override this method.</span>
<span class="sd">        - **parse_transcript()**: abstract method. you have to override this method.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset_path</span><span class="p">,</span> <span class="n">noiseset_size</span><span class="p">,</span> <span class="n">sample_rate</span><span class="o">=</span><span class="mi">16000</span><span class="p">,</span> <span class="n">noise_level</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">noise_augment</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">noise_augment</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">noise_injector</span> <span class="o">=</span> <span class="n">NoiseInjector</span><span class="p">(</span><span class="n">dataset_path</span><span class="p">,</span> <span class="n">noiseset_size</span><span class="p">,</span> <span class="n">sample_rate</span><span class="p">,</span> <span class="n">noise_level</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">parse_audio</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span>

    <span class="k">def</span> <span class="nf">parse_transcript</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span></div>


<div class="viewcode-block" id="SpectrogramParser"><a class="viewcode-back" href="../../../../Data.html#kospeech.data.audio.parser.SpectrogramParser">[docs]</a><span class="k">class</span> <span class="nc">SpectrogramParser</span><span class="p">(</span><span class="n">AudioParser</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parses audio file into (spectrogram / mel spectrogram / mfcc) with various options.</span>

<span class="sd">    Args:</span>
<span class="sd">        transform_method (str): which feature to use (default: mel)</span>
<span class="sd">        sample_rate (int): Sample rate of audio signal. (Default: 16000)</span>
<span class="sd">        n_mels (int):  Number of mfc coefficients to retain. (Default: 40)</span>
<span class="sd">        frame_length (int): frame length for spectrogram (ms) (Default : 20)</span>
<span class="sd">        frame_shift (int): Length of hop between STFT windows. (ms) (Default: 10)</span>
<span class="sd">        feature_extract_by (str): which library to use for feature extraction(default: librosa)</span>
<span class="sd">        del_silence (bool): flag indication whether to delete silence or not (default: True)</span>
<span class="sd">        input_reverse (bool): flag indication whether to reverse input or not (default: True)</span>
<span class="sd">        normalize (bool): flag indication whether to normalize spectrum or not (default:True)</span>
<span class="sd">        time_mask_para (int): Hyper Parameter for Time Masking to limit time masking length</span>
<span class="sd">        freq_mask_para (int): Hyper Parameter for Freq Masking to limit freq masking length</span>
<span class="sd">        time_mask_num (int): how many time-masked area to make</span>
<span class="sd">        freq_mask_num (int): how many freq-masked area to make</span>
<span class="sd">        sos_id (int): start of sentence token`s identification</span>
<span class="sd">        eos_id (int): end of sentence token`s identification</span>
<span class="sd">        target_dict (dict): dictionary of filename and labels</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">VANILLA</span> <span class="o">=</span> <span class="mi">0</span>           <span class="c1"># Not apply augmentation</span>
    <span class="n">SPEC_AUGMENT</span> <span class="o">=</span> <span class="mi">1</span>      <span class="c1"># SpecAugment</span>
    <span class="n">NOISE_INJECTION</span> <span class="o">=</span> <span class="mi">2</span>   <span class="c1"># Noise Injection</span>
    <span class="n">HYBRID_AUGMENT</span> <span class="o">=</span> <span class="mi">3</span>    <span class="c1"># Noise Injection &amp; SpecAugment</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">feature_extract_by</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;librosa&#39;</span><span class="p">,</span> <span class="n">sample_rate</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">16000</span><span class="p">,</span>
                 <span class="n">n_mels</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">80</span><span class="p">,</span> <span class="n">frame_length</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">20</span><span class="p">,</span> <span class="n">frame_shift</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span><span class="p">,</span>
                 <span class="n">del_silence</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">input_reverse</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
                 <span class="n">normalize</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>  <span class="n">transform_method</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;mel&#39;</span><span class="p">,</span>
                 <span class="n">time_mask_para</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">70</span><span class="p">,</span> <span class="n">freq_mask_para</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">12</span><span class="p">,</span> <span class="n">time_mask_num</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="n">freq_mask_num</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span>
                 <span class="n">sos_id</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">eos_id</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="n">target_dict</span><span class="p">:</span> <span class="nb">dict</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">noise_augment</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
                 <span class="n">dataset_path</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">noiseset_size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">noise_level</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.7</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SpectrogramParser</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">dataset_path</span><span class="p">,</span> <span class="n">noiseset_size</span><span class="p">,</span> <span class="n">sample_rate</span><span class="p">,</span> <span class="n">noise_level</span><span class="p">,</span> <span class="n">noise_augment</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">del_silence</span> <span class="o">=</span> <span class="n">del_silence</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_reverse</span> <span class="o">=</span> <span class="n">input_reverse</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">normalize</span> <span class="o">=</span> <span class="n">normalize</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sos_id</span> <span class="o">=</span> <span class="n">sos_id</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eos_id</span> <span class="o">=</span> <span class="n">eos_id</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">target_dict</span> <span class="o">=</span> <span class="n">target_dict</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spec_augment</span> <span class="o">=</span> <span class="n">SpecAugment</span><span class="p">(</span><span class="n">time_mask_para</span><span class="p">,</span> <span class="n">freq_mask_para</span><span class="p">,</span> <span class="n">time_mask_num</span><span class="p">,</span> <span class="n">freq_mask_num</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">transform_method</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;mel&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">transforms</span> <span class="o">=</span> <span class="n">MelSpectrogram</span><span class="p">(</span><span class="n">sample_rate</span><span class="p">,</span> <span class="n">n_mels</span><span class="p">,</span> <span class="n">frame_length</span><span class="p">,</span> <span class="n">frame_shift</span><span class="p">,</span> <span class="n">feature_extract_by</span><span class="p">)</span>

        <span class="k">elif</span> <span class="n">transform_method</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;mfcc&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">transforms</span> <span class="o">=</span> <span class="n">MFCC</span><span class="p">(</span><span class="n">sample_rate</span><span class="p">,</span> <span class="n">n_mels</span><span class="p">,</span> <span class="n">frame_length</span><span class="p">,</span> <span class="n">frame_shift</span><span class="p">,</span> <span class="n">feature_extract_by</span><span class="p">)</span>

        <span class="k">elif</span> <span class="n">transform_method</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;spect&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">transforms</span> <span class="o">=</span> <span class="n">Spectrogram</span><span class="p">(</span><span class="n">sample_rate</span><span class="p">,</span> <span class="n">frame_length</span><span class="p">,</span> <span class="n">frame_shift</span><span class="p">,</span> <span class="n">feature_extract_by</span><span class="p">)</span>

        <span class="k">elif</span> <span class="n">transform_method</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;fbank&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">transforms</span> <span class="o">=</span> <span class="n">FilterBank</span><span class="p">(</span><span class="n">sample_rate</span><span class="p">,</span> <span class="n">n_mels</span><span class="p">,</span> <span class="n">frame_length</span><span class="p">,</span> <span class="n">frame_shift</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupported feature : </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">transform_method</span><span class="p">))</span>

<div class="viewcode-block" id="SpectrogramParser.parse_audio"><a class="viewcode-back" href="../../../../Data.html#kospeech.data.audio.parser.SpectrogramParser.parse_audio">[docs]</a>    <span class="k">def</span> <span class="nf">parse_audio</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">audio_path</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">augment_method</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tensor</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Parses audio.</span>

<span class="sd">        Args:</span>
<span class="sd">             audio_path (str): path of audio file</span>
<span class="sd">             augment_method (int): flag indication which augmentation method to use.</span>

<span class="sd">        Returns: feature_vector</span>
<span class="sd">            - **feature_vector** (torch.FloatTensor): feature from audio file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">signal</span> <span class="o">=</span> <span class="n">load_audio</span><span class="p">(</span><span class="n">audio_path</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">del_silence</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">signal</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">augment_method</span> <span class="o">==</span> <span class="n">SpectrogramParser</span><span class="o">.</span><span class="n">NOISE_INJECTION</span> <span class="ow">or</span> <span class="n">augment_method</span> <span class="o">==</span> <span class="n">SpectrogramParser</span><span class="o">.</span><span class="n">HYBRID_AUGMENT</span><span class="p">:</span>
            <span class="n">signal</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">noise_injector</span><span class="p">(</span><span class="n">signal</span><span class="p">)</span>

        <span class="n">feature_vector</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">transforms</span><span class="p">(</span><span class="n">signal</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">normalize</span><span class="p">:</span>
            <span class="n">feature_vector</span> <span class="o">-=</span> <span class="n">feature_vector</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>

        <span class="c1"># Refer to &quot;Sequence to Sequence Learning with Neural Network&quot; paper</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_reverse</span><span class="p">:</span>
            <span class="n">feature_vector</span> <span class="o">=</span> <span class="n">feature_vector</span><span class="p">[:,</span> <span class="p">::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">feature_vector</span> <span class="o">=</span> <span class="n">FloatTensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ascontiguousarray</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="n">feature_vector</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">feature_vector</span> <span class="o">=</span> <span class="n">FloatTensor</span><span class="p">(</span><span class="n">feature_vector</span><span class="p">)</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">augment_method</span> <span class="o">==</span> <span class="n">SpectrogramParser</span><span class="o">.</span><span class="n">SPEC_AUGMENT</span> <span class="ow">or</span> <span class="n">augment_method</span> <span class="o">==</span> <span class="n">SpectrogramParser</span><span class="o">.</span><span class="n">HYBRID_AUGMENT</span><span class="p">:</span>
            <span class="n">feature_vector</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">spec_augment</span><span class="p">(</span><span class="n">feature_vector</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">feature_vector</span></div>

    <span class="k">def</span> <span class="nf">parse_transcript</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Soohwan Kim

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>